package com.lbd.system.service.impl;

import com.lbd.system.bean.Constant;
import com.lbd.system.dao.ConstantRepository;
import com.lbd.system.service.ConstantService;
import com.lbd.base.service.BaseServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service
@Transactional
public class ConstantServiceImpl extends BaseServiceImpl<Constant, ConstantRepository> implements ConstantService {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    ConstantRepository constantRepository;

    @Override
    public Specification<Constant> getSpecification(Constant constant) {
        return null;
    }

    /**
     * 构建查询语句和参数
     *
     * @param constant
     * @param parameter
     * @return
     */
    @Override
    public String buildJpql(Constant constant, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder(" select new Constant(id,code,label) from  Constant t   ");
        if (constant != null) {
            if (StringUtils.isNotBlank(constant.getCode())) {
                jpqlBuilder.append(" and  t.code like:code");
                parameter.put("code", constant.getCode().trim());
            }
            if (StringUtils.isNotBlank(constant.getLabel())) {
                jpqlBuilder.append(" and  t.label like :label");
                parameter.put("label", "%" + constant.getLabel().trim() + "%");
            }
            jpqlBuilder.append("");
        }
        jpqlBuilder.append(" order by createdDate desc ");
        return jpqlBuilder.toString();
    }


}
